<?php
ob_start();
define('LOUBIA_ZONE',true);

include_once('config.inc.php');

header('Content-Type: text/html; charset=' . $lb['charset']);

if ($lb['debug']){
	error_reporting(E_ALL);
}else{
	error_reporting(E_NONE);
}

include_once($lb['dir'] . 'include/calendar.inc.php');
include_once($lb['dir'] . 'include/calendar/' . $lb['calendar'] . '.inc.php');
include_once($lb['dir'] . 'include/db/' . $lb['db_type'] . '.inc.php');
include_once($lb['dir'] . 'include/db.inc.php');
include_once($lb['dir'] . 'include/session.inc.php');
include_once($lb['dir'] . 'include/cron.inc.php');
include_once($lb['dir'] . 'include/system.inc.php');
include_once($lb['dir'] . 'include/group.inc.php');
include_once($lb['dir'] . 'include/page.inc.php');
include_once($lb['dir'] . 'include/plugin.inc.php');
include_once($lb['dir'] . 'include/user.inc.php');
include_once($lb['dir'] . 'include/Smarty/libs/Smarty.class.php');
include_once($lb['dir'] . 'include/smarty.inc.php');
include_once($lb['dir'] . 'language/' . $lb['lang'] . '.inc.php');
include_once($lb['dir'] . 'language/admin/' . $lb['lang'] . '.inc.php');

// load english language (default)
if($lb['lang'] != 'en'){
	include_once($lb['dir'] . 'language/en.inc.php');
	include_once($lb['dir'] . 'language/admin/en.inc.php');
}

$lb['current_dir']		 = $_SERVER['PATH'];
$lb['current_file']		 = $_SERVER['PHP_SELF'];
$lb['current_full_file'] = $_SERVER['REQUEST_URI'];

$lb['db']		= new lb_db();
$lb['session'] 	= new lb_session();
//$lb['cron'] 	= new lb_cron();
$lb['user'] 	= new lb_user();
$lb['group']	= new lb_group();

if(!$lb['user']->is_su()){
	$lb['css']		= $lb['template_dir'] . $lb['template'] . '/' . $lb['main_css'];
	$lb['smarty'] 	= new lb_smarty('');
	lb_system::redirect($lb['webdir'], LANG_GO_HOME);
}else{
	$lb['smarty'] 	= new lb_smarty('/admin');
}

if(isset($_GET['task'])){
	$lb['task'] = trim($_GET['task']);
}else{
	$lb['task'] = false;
}

$lb['error']   = array();
$lb['content'] = false;

if(isset($_GET['action'])){
	$lb['action'] = trim($_GET['action']);
}else{
	$lb['action'] = false;
}

switch($lb['task']){
	case 'user':
		switch($lb['action']){
			case 'add_user':
				if(isset($_POST['submit'])){
					$group_id = lb_group::group_id(trim($_POST['group']));
					if($group_id != false){
						$user_id = $lb['user']->add_user(trim($_POST['username']), trim($_POST['password']), trim($_POST['email']), $group_id);
						if($user_id == false){
							$lb['error'][] = LANG_USER_ERROR_REG;
							$lb['task'] = 'error';
						}else{
							$lb['message'][] = LANG_USER_ADDED;
						}
					}else{
						$lb['error'][] = LANG_GROUP_ERROR_EXIST;
						$lb['task'] = 'error';
					}
				}
				$lb['error'] = array_merge($lb['error'], $lb['user']->get_errors());
				$groups = lb_group::list_groups();
				$lb['smarty']->assign('groups', $groups);
				
				break;
				
			case 'edit_user':
				if(isset($_POST['submit'])){
				
					if(!($user_id = $lb['user']->user_id(trim($_POST['username'])))){
						$lb['error'] = LANG_USER_ERROR_NOT_EXIST;
						$lb['task']  = 'error';
						break;
					}
					
					if($lb['user']->edit_user(trim($_POST['new_username']), 
											   trim($_POST['new_password']),
											   trim($_POST['new_email']),
											   $user_id) ){
						$lb['message'][] = LANG_USER_EDITED;
						
						if( ($group_name = lb_group::group_name($_POST['new_group'])) && 
							$lb['user']->edit_usergroup($user_id, $_POST['new_group']) ){
							$lb['message'][] = LANG_USER_GROUP_EDITED;
						}else{
							$lb['message'][] = LANG_USER_ERROR_GROUP_EDIT;
						}
						
					}else{
						$lb['error'] = array_merge($lb['error'], $lb['user']->get_errors());
						$lb['task']  = 'error';
					}
				}
				$groups = lb_group::list_groups();
				$lb['smarty']->assign('groups', $groups);
				
				break;
				
			case 'remove_user':
				if(isset($_POST['submit'])){
					$user_id = $lb['user']->user_id(trim($_POST['username']));
					if($user_id == false){
						$lb['error'][] = LANG_USER_ERROR_NOT_EXIST;
						$lb['task'] = 'error';				
					}else{
						if($lb['user']->remove_user($user_id) == true){
							$lb['message'][] = LANG_USER_REMOVED;
						}else{
							$lb['error'][] = LANG_USER_ERROR_REMOVE;
							$lb['task'] = 'error';
						}
					}
				}
				$lb['error'] = array_merge($lb['error'], $lb['user']->get_errors());
				break;
				
			default:
		}
		break;
	
	case 'first_page':
		$lb['page'] = new lb_page(1);
		if($lb['page']->type == false){
			$first_page = false;
			$lb['smarty']->assign('no_first_page', true);
		}else{	
			$first_page = true;
		}
		
		$lb['smarty']->assign('page', '1');
		
		if($lb['action'] == 'add'){
				if($first_page == true){
					$lb['error'][] = LANG_FIRST_PAGE_ERROR_EXIST;
					$lb['task'] = 'error';
					break;
				}
				if(isset($_GET['type'])){
				
					if(isset($_POST['submit'])){
						$type = 'type_' . $_GET['type'];
						include_once($lb['dir'] . 'page/' . $type . '/' . $type . '.inc.php');
						$lb['cpage'] = new $type();
						
						$add_page_id	= 1;
						$add_parent_id	= 0;
						
						if(isset($_POST['write']) && $_POST['write'] == 'on'){
							$write_perm	= true;
						}else{
							$write_perm	= false;
						}
						
						$page_id = $lb['cpage']->add_page ($_POST);
						
						if($page_id != false){
							$result	= $lb['page']->add_page (trim($_GET['type']), $page_id, $_POST['title'], $write_perm, $add_page_id, $add_parent_id);
						}else{
							$result = false;
						}
						
						if($result == true){
							$lb['message'][] = LANG_ADDED_PAGE;
						}else{
							$lb['error'][] 	 = LANG_ADD_PAGE_ERR;
							$lb['task'] 	 = 'error';
						}
					}else{
						$lb['smarty']->assign('add_page_type', trim($_GET['type']));
					}
					
				}else{
					$page_types = lb_page::list_pages();
					$lb['smarty']->assign('no_page_type', true);
					$lb['smarty']->assign ('page_types'	, $page_types);
				}
				
		}else{
			$lb['content'] = LANG_FIRST_PAGE_EXIST;
		}
		
		break;
	
	case 'group':

		switch($lb['action']){
			case 'add_group':
				if(isset($_POST['submit'])){
					if($lb['group']->add_group($_POST['group'])){
						$lb['message'][] = LANG_GROUP_ADDED;
					}else{
						$lb['error'] = array_merge($lb['error'], $lb['group']->get_errors());
						$lb['task']  = 'error';
					}
				}
				break;
				
			case 'edit_group':
				if(isset($_POST['submit'])){
					if($lb['group']->edit_group($_POST['group'], trim($_POST['new_name']))){
						$lb['message'][] = LANG_GROUP_EDITED;
					}else{
						$lb['error'] = array_merge($lb['error'], $lb['group']->get_errors());
						$lb['task']  = 'error';
					}
				}
				break;
				
			case 'remove_group':
				if(isset($_POST['submit'])){
					if($lb['group']->remove_group($_POST['group'])){
						$lb['message'][] = LANG_GROUP_REMOVED;
					}else{
						$lb['error'] = array_merge($lb['error'], $lb['group']->get_errors());
						$lb['task']  = 'error';
					}
				}
				break;
				
			default:
		}
		
		//List groups [group_id=>group_name]
		$groups = lb_group::list_groups();
		$lb['smarty']->assign('groups', $groups);
		
		break;
	case 'plugin':
		
		$lb['plugin'] = new lb_plugin();
		
		switch($lb['action']){
			//Add new plugin
			case 'add_plugin':
				if(isset($_POST['submit'])){
					if($lb['plugin']->add($_POST['plugin'], $_POST['title'], $_POST['hook'], $_POST['pages'])){
						lb_system::redirect($lb['webdir'] . 'admin.php?task=plugin', LANG_PLUGIN_ADDED);
					}else{
						$lb['message'] = LANG_PLUGIN_ADDING_ERROR;
					}
				}else{
					//Available plugins for install!
					$lb['smarty']->assign('available_plugins', $lb['plugin']->available_plugins());
				}

				break;
			
			//Remove plugin	
			case 'remove_plugin':
				if(isset($_GET['id'])){
					if($lb['plugin']->remove($_GET['id'])){
						lb_system::redirect($lb['webdir'] . 'admin.php?task=plugin', $_GET['title'] . ' ' . LANG_PLUGIN_REMOVED);
					}else{
						$lb['message'] = LANG_PLUGIN_REMOVING_ERROR . ' ' . $_GET['title'];
					}
					$lb['action'] = 'list_plugins';
					$installed_plugins = $lb['plugin']->installed_plugins(true);
					$lb['smarty']->assign('installed_plugins', $installed_plugins);
				}
				break;
				
			//Edit plugin's title and hooks
			case 'edit_plugin':
				if(isset($_POST['submit'])){
					
					if($lb['plugin']->edit($_POST['id'], $_POST['title'], $_POST['hook'], $_POST['pages'])){
						lb_system::redirect($lb['webdir'] . 'admin.php?task=plugin', LANG_PLUGIN_EDITED);
					}else{
						lb_system::redirect($lb['webdir'] . 'admin.php?task=plugin', LANG_PLUGIN_EDITING_ERROR);
					}
				}else{
					if(isset($_GET['id'])){
						$lb['smarty']->assign('edit_plugin', $lb['plugin']->info($_GET['id']));
					}else{
						lb_system::redirect($lb['webdir'] . 'admin.php?task=plugin', LANG_NO_PLUGIN);
					}
				}
				break;
			
			//Manage plugin
			case 'manage_plugin':
				$m_plugin = lb_plugin::info($_GET['id']);
				if(isset($_POST['submit'])){
					
				}else{
					$lb['smarty']->assign('plugin_name', $m_plugin['name']);
				}
				break;
			//List installed plugins
			case 'list_plugins':
			default:
				$installed_plugins = $lb['plugin']->installed_plugins(true);
				$lb['smarty']->assign('installed_plugins', $installed_plugins);
		}
		
		//Hooks
		$lb['smarty']->assign('hooks', $lb['plugin']->hooks());
		break;
		
	default:
		$count['page'] = lb_page::count();
		$count['user'] = lb_user::count();
		$count['on_user'] = lb_session::count(60*5);
		$lb['smarty']->assign('count', $count);
}

if($lb['task'] != false){
	$lb['smarty']->assign ('task'	, $lb['task']);
}
if($lb['content'] != false){
	$lb['smarty']->assign ('content', $lb['content']);
}
if($lb['action'] != false){
	$lb['smarty']->assign ('action'	, $lb['action']);
}

$lb['smarty']->assign ('script'			,$lb['current_file']);
$lb['smarty']->assign ('tpldir'			, $lb['template_dir'] . $lb['template'] . '/');
$lb['smarty']->assign ('webdir'			, $lb['webdir']);
$lb['smarty']->assign ('dir'			, $lb['dir']);
$lb['smarty']->assign ('language'		, $lb['lang']);
$lb['smarty']->assign ('logged'			, $lb['user']->logged());
$lb['smarty']->assign ('message'		, $lb['message']);
$lb['smarty']->assign ('error'			, $lb['error']);
$lb['smarty']->display('admin_index.tpl');

?>
